<template>

  <view>
    <!-- 使用自定义搜索组件 -->
    <my-search @click="searchHandler" class="pos" bgColor="#333" radius="3"></my-search>

    <!-- 轮播图区域 -->
    <swiper class="swiper" :indicator-dots="true" :autoplay="true" :interval="2000" :duration="1000" circular
      indicator-active-color="#ff0000">
      <swiper-item v-for="item in swiperList" :key="item.goods_id">
        <navigator class="swiper-item" :url="`/subpkg/details/details?goods_id=${item.goods_id}`">
          <image class="image" :src="item.image_src" mode=""></image>
        </navigator>
      </swiper-item>
    </swiper>

    <!-- 导航区域 -->
    <view class="nav">
      <view class="nav-item" v-for="(item,index ) in navList" :key="index" @click="cateHandle(item.name)">
        <image :src="item.image_src" mode=""></image>
      </view>
    </view>

    <!-- 楼层区域 -->
    <view class="floor">
      <view class="floor-item" v-for="item in floorList" :key="item.floor_title.name">
        <image class="floor-title-img" :src="item.floor_title.image_src" mode=""></image>
        <view class="img-group">
          <!-- 左侧大图 -->
          <view class="left-floor-img">
            <navigator :url="item.product_list[0].url">
              <image :src="item.product_list[0].image_src" :style="{width:item.product_list[0].image_width+'rpx'}"
                mode="widthFix" />
            </navigator>
          </view>
          <!-- 右侧四张小图 -->
          <view class="right-floor-img">
            <navigator :url="img.url" class="right-img-item" v-for="(img,index) in item.product_list" :key="img.name">
              <image v-if="index!==0" :src="img.image_src" :style="{width:img.image_width+'rpx'}" mode="widthFix">
              </image>
            </navigator>
          </view>
        </view>
      </view>
    </view>

  </view>



</template>

<script setup lang="ts">
  import {
    onMounted, ref,
  } from 'vue';
  import { FloorElement, NavElement, ProductListElement, SwiperElement } from './type';
  import { getSwiperList } from '../../api/home'
  import { showMsg } from '../../utils/interact';
  import { escook } from '../../utils/escook';
  import {setTabbarBadge} from '../../mixins/cart-badge'


  const swiperList = ref<SwiperElement[]>([]);
  const navList = ref<NavElement[]>([]);
  const floorList = ref<FloorElement[]>([]);

  //页面初始化时调用获取轮播图数据的方法
  onMounted(() => {
    getSwiperData();
    getNavList();
    getFloorData();
  })

  setTabbarBadge();
  //获取轮播图数据的方法
  async function getSwiperData() {
    const { data: res } = await getSwiperList();
    //请求失败
    if (res.meta.status !== 200) return showMsg();

    //请求成功保存轮播图数据
    swiperList.value = res.message;
  }
  //获取导航栏图片的方法
  async function getNavList() {
    const { data: res } = await escook.get('api/public/v1/home/catitems');

    //请求失败
    if (res.meta.status !== 200) return showMsg();

    //请求成功保存轮播图数据
    navList.value = res.message;
  }
  //获取楼层数据的函数
  async function getFloorData() {
    const { data: res } = await escook.get('api/public/v1/home/floordata');
    if (res.meta.status === 200) {
      //对数据进行处理
      res.message.forEach((floor : FloorElement) => {
        floor.product_list.forEach((prod : ProductListElement) => {
          //@ts-ignore
          prod.url = '/subpkg/goods_list/goods_list?' + prod.navigator_url.split("?")[1];
        })
      })
      floorList.value = res.message;
    } else {
      showMsg();
    }
  }

  //事件处理函数组
  function cateHandle(name : string) {
    if (name === '分类') {
      uni.switchTab({
        url: '/pages/cate/cate'
      })
    }
  }

  function searchHandler() {
    uni.navigateTo({
      url: '/subpkg/search/search'
    })
  }
</script>


<style lang="scss" scoped>
  //自定义搜索组件
  .pos {
    width: 100vw;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 99;
  }

  // <!-- 轮播图区域 -->
  .swiper {
    margin-top: 50px;
    width: 100vw;
    height: 330rpx;

    .swiper-item,
    .image {
      width: 100%;
      height: 100%;
    }
  }

  // <!-- 导航区域 -->
  .nav {
    display: flex;
    height: 140rpx;
    justify-content: space-between;
    margin: 15px 0;

    .nav-item,
    image {
      width: 120rpx;
      height: 100%;
    }
  }


  //楼层区域
  .floor {

    // 标题
    .floor-title-img {
      width: 100%;
      height: 120rpx;
    }

    //图片群
    .img-group {
      display: flex;
      padding-left: 14rpx;

      //左侧大图
      .left-floor-img {}

      //右侧四张小图
      .right-floor-img {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-around;
      }
    }

  }
</style>